package com.lvmama.dashboard.rop.service.search;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.lvmama.dashboard.core.annotation.AjaxMethod;
import com.lvmama.dashboard.dao.search.ILogSearchDao;
import com.lvmama.dashboard.entity.ApiSummary;
import com.lvmama.dashboard.model.page.Page;
import com.lvmama.dashboard.model.page.PageResult;
import com.lvmama.dashboard.model.rop.AjaxRequestBody;
import com.lvmama.dashboard.model.rop.AjaxResponse;
import com.lvmama.dashboard.model.rop.search.request.LogSearchRequest;
import com.lvmama.dashboard.model.rop.search.response.LogSearchDetail;
import com.lvmama.dashboard.model.rop.search.response.LogSearchResponse;
import com.lvmama.dashboard.service.ajax.search.ILogSearchService;
import com.lvmama.dashboard.service.api.IApiSummaryService;
import com.lvmama.java.rhino.spark.tables.ElasticLogBaseParam;
import com.lvmama.java.rhino.spark.utils.ElasticUtils;
import com.lvmama.java.rhino.spark.utils.ElasticUtils.ElasticSearchResult;

@Component("logSearchService")
public class LogSearchServiceImpl implements ILogSearchService {
	
	@Autowired
	private ILogSearchDao logSearchDao;
	@Autowired
	private IApiSummaryService apiSummaryService;
	
	@Override
	@AjaxMethod(ajaxmethod = "com.lvtu.dashboard.search.log")
	public AjaxResponse<LogSearchResponse> searchLog(AjaxRequestBody<LogSearchRequest> requestBody) {
		LogSearchRequest request = requestBody.getT();
		int page = request.getPage();
		if(page<=0) {
			request.setPage(1);
		}
		int pageSize = request.getPageRows();
		if(pageSize <= 0) {
			request.setPageRows(20);
		}
		PageResult<ElasticSearchResult<ElasticLogBaseParam>> queryResult = logSearchDao.queryLog(request);
		List<ElasticSearchResult<ElasticLogBaseParam>> resultList = queryResult.getList();
		List<LogSearchDetail> logs = new ArrayList<LogSearchDetail>();
		if(resultList != null) {
			for(ElasticSearchResult<ElasticLogBaseParam> tempResult : resultList) {
				ElasticLogBaseParam logBaseParam = tempResult.getData();
				String[] highlightTexts = tempResult.getHighlightTexts();
				List<String> logList = logBaseParam.getLogDetail();
				logList = replaceHighlightText(logList, highlightTexts);
				logBaseParam.setLogDetail(logList);
				LogSearchDetail searchDetail = new LogSearchDetail();
				BeanUtils.copyProperties(tempResult.getData(), searchDetail);
				logs.add(searchDetail);
			}
		}
		AjaxResponse<LogSearchResponse> responseBody = new AjaxResponse<LogSearchResponse>();
		LogSearchResponse response = new LogSearchResponse();
		response.setLogs(logs);
		Page pageObj = new Page(queryResult.getPage(), queryResult.getPageSize(), queryResult.getTotalSize());
		response.setPage(pageObj);
		responseBody.setData(response);
		
		saveApiToDb(logs);
		
		return responseBody;
	}

	private void saveApiToDb(List<LogSearchDetail> logs) {
		if(CollectionUtils.isEmpty(logs)) {
			return;
		}
		List<ApiSummary> list = new ArrayList<ApiSummary>();
		for(LogSearchDetail logSearchDetail : logs) {
			ApiSummary apiSummary = new ApiSummary();
			apiSummary.setApi(logSearchDetail.getMethod());
			apiSummary.setVersion(logSearchDetail.getVersion());
			list.add(apiSummary);
		}
		apiSummaryService.saveApiSummary(list);
	}

	private List<String> replaceHighlightText(List<String> logList, String[] highlightTexts) {
		if(highlightTexts == null || highlightTexts.length == 0) {
			return logList;
		}
		for (int j = 0; j < highlightTexts.length; j++) {
			String highlightText = highlightTexts[j];
			highlightText = highlightText.replace(ElasticUtils.HIGHLIGHTER_POST_TAGS, "").replace(ElasticUtils.HIGHLIGHTER_PRE_TAGS, "");
			for (int i = 0; i < logList.size(); i++) {
				String log = logList.get(i);
				if(log.indexOf(highlightText) == -1) {
					continue;
				}
				log = log.replace(highlightText, "{" + j +"}");
				logList.add(i, log);
				break;
			}
		}
		int index = 0;
		for (int i = 0; i < logList.size(); i++) {
			String log = logList.get(i);
			String replaceStr = "{" + index + "}";
			if(log.indexOf(replaceStr) == -1) {
				continue;
			}
			log = log.replace(replaceStr, highlightTexts[index]);
			logList.add(index, log);
			System.out.println("index:" + index);
			index++;
			i = 0;
		}
		return logList;
	}


}
